//app.js
import WxRequest from '/utils/wx-request/lib/index.js'

const host = require('./config').host
App({

    onLaunch: function() {
        this.globalData.userInfo = wx.getStorageSync('userInfo');

        console.log("执行onLaunch")

        console.log("初始化数据字典")

        wx.setStorageSync("dataDrict", {
            order_status_10: '待付款',
            order_status_20: '待接单',
            order_status_30: '待发货',
        });

        this.WxRequest.interceptors.use({
            // 请求数据
            request(request) {
                let sessionId = wx.getStorageSync('sessionId');
                if (sessionId == '') {
                    sessionId = 'unlogin';
                }
                console.log('sessionId:', sessionId);
                request.header.sessionId = sessionId;

                //是否需要loading
                if (request.data && request.data.loading){
                    wx.showLoading({
                        title: '加载中...',
                        mask:true
                    })
                    delete request.data.loading;
                }

                console.info("request=%o", request)
                
                return request
            },
            // 请求失败
            requestError(requestError) {
                wx.hideLoading()
                wx.showModal({
                    content: '服务器异常,请稍候再试',
                    showCancel: false,
                    success: function(res) {
                        if (res.confirm) {

                        }
                    }
                });
                return Promise.reject(requestError)
            },
            // 响应数据
            response(response) {
                setTimeout(function(){
                    wx.hideLoading()
                },500)
                console.log('response=%o', response);
                if (response.data.code == '-1') {
                    wx.showToast({
                        title: '您还未登录',
                        icon: 'none',
                        mask: true,
                        success: function(){
                            wx.removeStorageSync("sessionId");
                            wx.removeStorageSync("userInfo");
                            setTimeout(function () {
                                wx.navigateTo({
                                    url: '/pages/my_login/index',
                                })
                            }, 1500)
                        }
                    })
                }
                return response
            },
            // 响应失败
            responseError(responseError) {
                wx.hideLoading()
                wx.showModal({
                    content: '服务器异常,请稍候再试',
                    showCancel: false,
                    success: function(res) {
                        if (res.confirm) {

                        }
                    }
                });
                return Promise.reject(responseError)
            },
        })
    },
    globalData: {
        userInfo: null
    },
    server: {
        addr: host
    },
    WxRequest: new WxRequest({
        baseURL: host,
    }),

    showModal(error) {
        //简单showModal
        wx.showModal({
            content: error.msg,
            showCancel: false,
        })
    }


})